// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.commands;

import org.dengues.core.metadata.MetadataHelper;
import org.dengues.core.metadata.MetadataUtils;
import org.dengues.core.scripts.ScriptResultBean;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.model.DenguesModelManager;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.EMFDataType;
import org.dengues.model.database.ForeignKey;
import org.dengues.model.database.ForeignKeyLabel;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.requests.CreateConnectionRequest;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-12 qiang.zhang $
 * 
 */
public class ForeignKeyCreateCommand extends AbstractGEFCommand {

    private DBColumn source;

    private DBColumn target = null;

    private final CreateConnectionRequest request;

    /**
     * Qiang.Zhang.Adolf@gmail.com ForgeinKeyCreateCommand constructor comment.
     * 
     * @param source
     * @param request
     */
    public ForeignKeyCreateCommand(DBColumn source, CreateConnectionRequest request) {
        setLabel(Messages.getString("ForgeinKeyCreateCommand.Label")); //$NON-NLS-1$
        this.source = source;
        this.request = request;

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getSource".
     * 
     * @return
     */
    public DBColumn getSource() {
        return this.source;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#canExecute()
     */
    @Override
    public boolean canExecute() {
        return ScriptResultBean.canExecute(getSource(), getTarget());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {
        if (request.getNewObject() instanceof ForeignKey) {
            DatabaseDiagram dbDiagram = getSource().getTable().getDbDiagram();
            EList<ForeignKey> inputs = getTarget().getInputs();
            if (dbDiagram.getEmfType().equals(EMFDataType.MAPPER)) {
                for (ForeignKey foreignKey : inputs) {
                    foreignKey.getSource().getOutputs().remove(foreignKey);
                }
                inputs.clear();
            }
            ForeignKey key = (ForeignKey) request.getNewObject();
            key.setName(MetadataHelper.generateNewForeignKeyName(getSource()));
            key.setSource(getSource());
            key.setTarget(getTarget());
            getSource().setForeignKey(true);
            getSource().getOutputs().add(key);
            inputs.add(key);

            if (!dbDiagram.getEmfType().equals(EMFDataType.MAPPER)) {
                ForeignKeyLabel fkLabel = DenguesModelManager.getDatabaseFactory().createForeignKeyLabel();
                fkLabel.setName(key.getName());
                String foreignKeyUname = MetadataUtils.getForeignKeyUname(key);
                Point point = MetadataHelper.getColumnLocation(key);
                fkLabel.setX(point.x);
                fkLabel.setY(point.y);

                fkLabel.getProperties().put(MetadataUtils.FK_NAME, foreignKeyUname);
                dbDiagram.getDbChildren().add(fkLabel);
            } else {
                String res = getSource().getTable().getName() + "." + getSource().getName();
                getTarget().setDefaultValue(ScriptResultBean.getTrimLinkName(res));
                getTarget().setLinkName(res);
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setSource".
     * 
     * @param source
     */
    public void setSource(DBColumn source) {
        this.source = source;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getTarget".
     * 
     * @return
     */
    public DBColumn getTarget() {
        return this.target;
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setTarget".
     * 
     * @param target
     */
    public void setTarget(DBColumn target) {
        this.target = target;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.editors.AbstractGEFCommand#undo()
     */
    @Override
    public void undo() {
        if (request.getNewObject() instanceof ForeignKey) {
            ForeignKey key = (ForeignKey) request.getNewObject();
            key.setSource(getSource());
            key.setTarget(getTarget());
            getSource().setForeignKey(false);
            getSource().getOutputs().remove(key);
            getTarget().getInputs().remove(key);
        }
    }
}
